package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.DisplayImageActivity;
import com.hxkcloud.patient.beans.ImageBean;
import com.hxkcloud.patient.beans.UchatBean;
import com.hxkcloud.patient.config.UrlConfig;
import com.hxkcloud.patient.views.SuperGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/26.
 */

public class PictureConsultAdapter extends BaseAdapter {

    private Context context;
    private List<UchatBean> data;
//    private ImageSupGridAdapterchart imageAdapter;
    public PictureConsultAdapter(Context context, List<UchatBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolders holders = null;
        if (convertView==null){
            convertView = View.inflate(context,R.layout.pictureconsultadapter_item,null);
            holders = new ViewHolders();
            holders.tv_left_time = (TextView) convertView.findViewById(R.id.left_time);
            holders.tv_left_content = (TextView) convertView.findViewById(R.id.left_content);
            holders.left_gridview = (SuperGridView) convertView.findViewById(R.id.recyclerview_image_left);
            holders.ll_left = (LinearLayout) convertView.findViewById(R.id.layout_left_answer);
            holders.ll_send = (LinearLayout) convertView.findViewById(R.id.layout_right_send);
            holders.tv_right_time = (TextView) convertView.findViewById(R.id.send_time);
            holders.tv_right_content = (TextView) convertView.findViewById(R.id.send_content);
            holders.send_gridview = (SuperGridView) convertView.findViewById(R.id.recyclerview_image_send);
            convertView.setTag(holders);
        }else
        {
            holders = (ViewHolders) convertView.getTag();
        }

        final ViewHolders currentHolder = holders;


        if (data.get(position).getChat().equals("0")){
            holders.ll_send.setVisibility(View.VISIBLE);
            holders.tv_right_time.setText(data.get(position).getTime());
            holders.tv_right_content.setText(data.get(position).getContent());
            holders.send_gridview.setNumColumns(1);
            List<ImageBean> imageBeanlist = new ArrayList<>();
            if (data.get(position).getImages()!=null){
                for (int i=0;i<data.get(position).getImages().size();i++){
                    ImageBean imageBean = new ImageBean();
                    imageBean.setImageType(2);
                    String imgPath = data.get(position).getImages().get(i).replace("\"","");
                    imageBean.setImage(UrlConfig.image_host + imgPath);
                    imageBeanlist.add(imageBean);
                }

                currentHolder.imageAdapter = new ImageSupGridAdapterchart(imageBeanlist,context,0);

                currentHolder.imageAdapter.setOnItemClickListener(new ImageSupGridAdapterchart.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(context, DisplayImageActivity.class);
                        intent.putExtra("path", currentHolder.imageAdapter.getImageBeen(position).getImage());
                        intent.putExtra("title", (position + 1) + "/" + currentHolder.imageAdapter.getCount());
                        intent.putExtra("position", position);
                        intent.putStringArrayListExtra("pics", (ArrayList<String>) currentHolder.imageAdapter.getImageBeen());
                        intent.putExtra("isHideDelete", "true");
                        context.startActivity(intent);
                    }
                });
                holders.send_gridview.setAdapter(currentHolder.imageAdapter);

            } else {
                holders.send_gridview.setVisibility(View.GONE);
            }
        }//else {
        if (data.get(position).getChat().equals("1")){
            holders.ll_left.setVisibility(View.VISIBLE);
            holders.tv_left_time.setText(data.get(position).getTime());
            holders.tv_left_content.setText(data.get(position).getContent());
            holders.left_gridview.setNumColumns(1);
            List<ImageBean> imageBeanlist = new ArrayList<>();
            Log.i("-----imgpaath","igm"+data.get(position).getImages().size());
            if (data.get(position).getImages()!=null){
                for (int i=0;i<data.get(position).getImages().size();i++){
                    ImageBean imageBean = new ImageBean();
                    imageBean.setImageType(2);
                    String imgPath = data.get(position).getImages().get(i).replace("\"","");
                    imageBean.setImage(UrlConfig.image_host + imgPath);
                    imageBeanlist.add(imageBean);
                }
                currentHolder.imageAdapter = new ImageSupGridAdapterchart(imageBeanlist,context,1);
                currentHolder.imageAdapter.setOnItemClickListener(new ImageSupGridAdapterchart.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(context, DisplayImageActivity.class);
                        intent.putExtra("path", currentHolder.imageAdapter.getImageBeen(position).getImage());
                        intent.putExtra("title", (position + 1) + "/" + currentHolder.imageAdapter.getCount());
                        intent.putExtra("position", position);
                        intent.putStringArrayListExtra("pics", (ArrayList<String>) currentHolder.imageAdapter.getImageBeen());
                        intent.putExtra("isHideDelete", "true");
                        context.startActivity(intent);
                    }
                });
                holders.left_gridview.setAdapter(currentHolder.imageAdapter);
            }else {
                holders.left_gridview.setVisibility(View.VISIBLE);
            }

        }

        return convertView;
    }
    static class ViewHolders{
        LinearLayout ll_send;
        TextView tv_right_time;
        TextView tv_right_content;
        SuperGridView send_gridview;
        LinearLayout ll_left;
        TextView tv_left_time;
        TextView tv_left_content;
        SuperGridView left_gridview;
        ImageSupGridAdapterchart imageAdapter;

    }
}
